package com.sx.gongzhong.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sx.gongzhong.R;
import com.sx.gongzhong.activity.MessageDynamicActivity;
import com.sx.gongzhong.activity.MessageNotifyActivity;
import com.sx.gongzhong.model.MsgEvent;
import com.sx.gongzhong.net.MyRxBus;
import com.sx.gongzhong.pest.MessageUnreadNumPest;
import com.sx.gongzhong.utils.ToastUtils;
import com.sx.gongzhong.view.MessageUnreadNumView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by XZY on 2017/12/28.
 * 我的
 */
public class MessageFragment extends LazyFragment implements MessageUnreadNumView {

    @BindView(R.id.tv_dynamicmessge)
    TextView tvDynamicmessge;
    @BindView(R.id.tv_notifymessge)
    TextView tvNotifymessge;
    private Disposable disposable;
    private boolean isPrepared=false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_message, container, false);
        ButterKnife.bind(this, view);
        initRx();
        isPrepared=true;
        return view;
    }

    private void getUnreadNum() {
        new MessageUnreadNumPest(this).getNotifyUnreadNum();
        new MessageUnreadNumPest(this).getDynamicUnreadNum();
    }

    private void initRx() {
        disposable =  MyRxBus.get().toObservable(MsgEvent.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<MsgEvent>() {
                    @Override
                    public void accept(MsgEvent msgEvent) throws Exception {
                        if ("MessageFragment".equals(msgEvent.getRequest())) {
                            if (msgEvent.getType()== 1){
                                getUnreadNum();
                            }
                        }
                    }
                });
    }

    @OnClick({R.id.rl_dynamicmessge, R.id.rl_notifymessge})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_dynamicmessge:
                startActivity(new Intent(getActivity(), MessageDynamicActivity.class));
                break;
            case R.id.rl_notifymessge:
                startActivity(new Intent(getActivity(), MessageNotifyActivity.class));
                break;
        }
    }

    @Override
    protected void lazyLoad() {
        if(isPrepared && isVisible) {
           isPrepared = false;
            getUnreadNum();
        }
    }

    @Override
    public void showError(String message) {
        ToastUtils.showToast(getActivity(),message);
    }

    @Override
    public void getDynamicUnreadNum(String s) {
        if (tvDynamicmessge != null){
            tvDynamicmessge.setText(s);
        }
    }

    @Override
    public void getNotifyUnreadNum(String s) {
        if (tvNotifymessge != null){
            tvNotifymessge.setText(s);
        }
    }

    @Override
    public void onDestroyView() {
        if (disposable != null){
            disposable.dispose();
        }
        isPrepared=false;
        super.onDestroyView();
    }

}
